package com.qf.service.impl;

import com.qf.bean.Teacher;
import com.qf.dao.TeacherDao;
import com.qf.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * IntelliJ IDEA -> springdata
 * 2019/11/30 19:39
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherDao teacherDao;
    @Override
    public Teacher findById(int tid) {
        return  teacherDao.findOne(tid);
    }
@Transactional
    @Override
    public Teacher  insertTeacher(Teacher teacher) {
        Teacher teacher1 = teacherDao.save(teacher);
//        System.out.println(55/0);
        return teacher1;
    }

    @Override
    public Teacher updateTeacher(Teacher teacher) {
        return teacherDao.save(teacher);
    }

    @Override
    public List<Teacher> findAll() {
        Iterable<Teacher> iterables = teacherDao.findAll();
        Iterator<Teacher> iterators = iterables.iterator();
        ArrayList<Teacher> teachers = new ArrayList<>();
        while (iterators.hasNext()){
            Teacher teacher = iterators.next();
            teachers.add(teacher);
        }
        return  teachers;
    }

    @Override
    public List<Teacher> findByName(String tname) {
        return teacherDao.findByTnameLike(tname);
    }

    @Override
    public List<Teacher> findByTnameAndTsex(String tname, String tsex) {
        return teacherDao.findByTnameAndTsex(tname,tsex);
    }

    @Override
    public List<Teacher> findByTids(int begin, int end) {
        return teacherDao.findByTidBetween(begin,end);
    }

    @Override
    public List<Teacher> findByTidGreaterThan(int tid) {
        return teacherDao.findByTidGreaterThan(tid);
    }

    @Override
    public List<Teacher> findByTnameContaining(String tname) {
        return teacherDao.findByTnameContaining(tname);
    }

    @Override
    public List<Teacher> findByTnameStartingWith(String tname) {
        return teacherDao.findByTnameStartingWith(tname);
    }

    @Override
    public List<Teacher> findByTidIn(List<Integer> ids) {
        return teacherDao.findByTidIn(ids);
    }

    @Override
public int countByTnameContaining(String tname) {
        return teacherDao.countByTnameContaining(tname);
    }

    @Override
    public List<Teacher> findAll(Sort sort) {
        return teacherDao.findAll(sort);
    }

    @Override
    public Page<Teacher> findByTidIn(List list, Pageable pageable) {
        return teacherDao.findByTidIn(list,pageable);
    }
}
